package com.jdk.learn.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 内存栅栏  可以循环使用
 */
public class MultiThreadCyclicBarrier extends Thread{
    private CyclicBarrier cyclicBarrier;
    public MultiThreadCyclicBarrier(CyclicBarrier cyclicbarrier) {
        this.cyclicBarrier=cyclicbarrier;
    }

    @Override
    public void run() {
        try {
            System.out.println("子线程"+getName()+"begin");
            Thread.sleep(30);
            System.out.println("子线程"+getName()+"end");
            cyclicBarrier.await();

            System.out.println("子线程"+getName()+"同时处理其他。。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
            CyclicBarrier cyclicBarrier=new CyclicBarrier(5);
        for (int i = 0; i < 5; i++) {
            new MultiThreadCyclicBarrier(cyclicBarrier).start();
        }
    }
}
